//
//  BCProjectDetailJoinPeopleCell.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/22.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCProjectDetailJoinPeopleCell.h"
//
#import "BCJoinPeopleCollectionLayout.h"
#import "BCJoinPeopleCollectionCell.h"

#import <OCSkeleton.h>


#import "BCProjectDetailResponse.h"

static CGFloat BCProjectDetailJoinPeopleCellTopPadding = 10.0;
static CGFloat BCProjectDetailJoinPeopleCellBottomPadding = 25.0;
//
static CGFloat BCProjectDetailJoinPeopleCellConentPadding = 3.0;


@interface BCProjectDetailJoinPeopleCell () <OCGradientsOwner,UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UILabel *joinLabel;

/**
 *BCJoinPeopleCollectionLayout
 */
@property (nonatomic, strong) BCJoinPeopleCollectionLayout *collectionLayout;
@property (nonatomic, strong) UICollectionView *joinCollection;


/**
 *moreButton
 */
@property (nonatomic, strong) QMUIButton *moreButton;

/**
 *labelContainerView
 */
@property (nonatomic, strong) OCGradientContainerView *labelContainerView;


/**
 *joinSource
 */
@property (nonatomic, copy) NSArray *joinSource;

@end

@implementation BCProjectDetailJoinPeopleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubviews];
        [self setContainerViews];
    }
    return self;
}
- (void)setupSubviews {
    self.joinLabel = [UILabel new];
    self.joinLabel.font = [self.class joinLabelFont];
    self.joinLabel.textColor = [UIColor whiteColor];
    self.joinLabel.textAlignment = NSTextAlignmentCenter;
    self.joinLabel.numberOfLines = 2;
    [self.contentView addSubview:self.joinLabel];
    [self.joinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(BCProjectDetailJoinPeopleCellTopPadding);
        make.centerX.mas_equalTo(self.contentView);
        make.width.mas_equalTo(self.contentView).offset(-60.0);
        make.height.mas_equalTo([self.class joinLabelHeight]);
    }];
    //
    self.collectionLayout = [BCJoinPeopleCollectionLayout new];
    self.joinCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.collectionLayout];
    self.joinCollection.dataSource = self;
    self.joinCollection.delegate = self;
    self.joinCollection.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.joinCollection];
    [self.joinCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.joinLabel.mas_bottom).offset(BCProjectDetailJoinPeopleCellConentPadding);
        make.centerX.mas_equalTo(self);
        make.width.mas_equalTo(self.contentView).offset(-60.0);
        make.height.mas_equalTo([BCJoinPeopleCollectionLayout itemDefaultSize].height);
    }];
    //
    [self.joinCollection registerClass:BCJoinPeopleCollectionCell.class forCellWithReuseIdentifier:NSStringFromClass(BCJoinPeopleCollectionCell.class)];
    
    //
    self.moreButton = [QMUIButton new];
    [self.moreButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.moreButton.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
    [self.contentView addSubview:self.moreButton];
    [self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.joinCollection);
        make.height.mas_equalTo(self.joinCollection);
        make.left.mas_equalTo(self.joinCollection.mas_centerX);
    }];
}

- (void)setContainerViews {
    self.labelContainerView = [OCGradientContainerView new];
    [self.contentView addSubview:self.labelContainerView];
    [self.labelContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.joinLabel);
        make.centerY.mas_equalTo(self.joinLabel);
        make.width.mas_equalTo(self.joinLabel).multipliedBy(.5);
        make.height.mas_equalTo(self.joinLabel.font.lineHeight);
    }];
    //
}


#pragma mark - UICollectionViewDataSource,UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.cellData) {//最多10个
        return self.joinSource.count;
    }else {
        return 0;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BCJoinPeopleCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BCJoinPeopleCollectionCell class]) forIndexPath:indexPath];
    cell.head_img = self.joinSource[indexPath.row];
    return cell;
}


- (void)setCellData:(BCProjectDetailData *)cellData {
    _cellData = cellData;
    if (cellData) {
        self.labelContainerView.alpha = 0;
        //
        self.joinLabel.text = cellData.flow.partake;
        //
        NSInteger maxNumber = 10;
        NSMutableArray *joinSource = [NSMutableArray arrayWithCapacity:10];
        NSArray *joins = cellData.data.join_users;
        for (int i=0; i<joins.count; i++) {
            if (i==maxNumber) {
                break;
            }
            [joinSource addObject:joins[i]];
        }
        self.joinSource = joinSource;
        
        //
        self.moreButton.alpha = joins.count>maxNumber?1:0;
        //layoutButton
        [self.moreButton setTitle:@"•••" forState:UIControlStateNormal];
    }else {
        self.joinLabel.text = nil;
        //默认设置几个count 来表示collection的 ContainerView
        self.labelContainerView.alpha = 1;
        //
        self.joinSource= nil;
        self.moreButton.alpha = 0;
    }
    [self.joinCollection reloadData];
    //
    [self.moreButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.joinCollection.mas_centerX).offset(self.collectionLayout.contentWidth/2+6.0);
    }];
}


#pragma mark - OCGradientsOwner
- (NSArray <OCGradientLayer *>*)gradientLayers {
    if (self.labelContainerView) {
        return @[self.labelContainerView.gradientLayer];
    }
    return nil;
}

- (NSArray <UIView *>*)skeletonViews {
    if (self.labelContainerView) {
        return @[self.labelContainerView];
    }
    return nil;
}


+ (UIFont *)joinLabelFont {
    return [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
}
+ (CGFloat)joinLabelHeight {
    return [self joinLabelFont].lineHeight*2+1.0;
}

+ (CGFloat)defaultHeight {
    CGFloat height = 0;
    height += BCProjectDetailJoinPeopleCellTopPadding;
    height += [self joinLabelHeight];
    height += BCProjectDetailJoinPeopleCellConentPadding;
    height += [BCJoinPeopleCollectionLayout itemDefaultSize].height;
    height += BCProjectDetailJoinPeopleCellBottomPadding;
    return height;
}

@end
